package com.gy.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * TODO: document your custom view class.
 */
public class SpiderLayout extends View {
    private Paint mPaint;
    private Path mPath;
    private int mWidth;
    private int mHeight;
    private float unit_len;
    private int boderOffset =4;

    public SpiderLayout(Context context) {
        super(context);
        init(null, 0);
    }

    public SpiderLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public SpiderLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        mWidth=MeasureSpec.getSize(widthMeasureSpec);
        unit_len = (mWidth-boderOffset) / 2f;
        mHeight = (int) ((Math.cos(Math.PI / 6d) * 2 * unit_len)+boderOffset);
        setMeasuredDimension(mWidth, mHeight);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(boderOffset);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth/2f, mHeight/2f);
        canvas.scale(1, -1);
        mPath.moveTo(unit_len, 0);
        mPath.lineTo(unit_len / 2f, (mHeight-boderOffset)/2f);
        mPath.lineTo(-unit_len / 2f, (mHeight-boderOffset)/2f);
        mPath.lineTo(-unit_len,0);
        mPath.lineTo(-unit_len / 2f, -(mHeight-boderOffset)/2f);
        mPath.lineTo(unit_len / 2f, -(mHeight-boderOffset)/2f);
        mPath.close();
        canvas.drawPath(mPath,mPaint);
        for (int i = 0; i < 3; i++) {
            canvas.scale(0.7f,0.7f);
            mPaint.setStrokeWidth(mPaint.getStrokeWidth()/0.7f);
            canvas.drawPath(mPath,mPaint);
        }
    }
}
